/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.plant.daoImpl;

import com.plant.dao.BaseDao;
import java.io.Serializable;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

/**
 * @author JAVA工程师--李琦
 * 			下面是DAO实现层 主要的实现的方法	
 */
@Repository
public class BaseDaoImpl extends HibernateDaoSupport implements BaseDao {

	public List loadAll(Class entityClass) {// 1.加载所有方法
		//super.getHibernateTemplate().loadAll(entityClass);
		return null;
	}

	public List<Object> findByHql(String queryString, Object[] values) { // 2通过hql语句查询

		return super.getHibernateTemplate().find(queryString, values);
	}

	@Resource(name = "sessionFactory")
	public final void setSessionFactoryMe(SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}

	public boolean add(Object o) { // 4.添加方法。添加对象
		boolean isAdd = true; // 是否添加成功
		try {
			super.getHibernateTemplate().save(o);
		} catch (Exception e) {
			e.printStackTrace();
			isAdd = false;
		}
		return isAdd;
	}

	public boolean del(Object o) { // 5.删除对象 删除方法
		boolean isDel = true;// 是否删除成功
		try {
			super.getHibernateTemplate().delete(o);
		} catch (Exception e) {
			e.printStackTrace();
			isDel = false;
		}
		return isDel;
	}

	public boolean update(Object o) { // 6.修改方法 修改对象
		boolean isUpdate = true; // 判断是否修改成功
		try {
			super.getHibernateTemplate().update(o);
		} catch (Exception e) {
			isUpdate = false;
		}
		return isUpdate;
	}

	public List<Object> find(String queryString, Object values) { // --7.公共的方法通过hql语句查找
		return super.getHibernateTemplate().find(queryString, values);
	}

	@SuppressWarnings("unchecked")
	public Object find(Class c, Serializable key) { // --8。通过实体类的关键字查询
		Object o = null;
		try {
			o = super.getHibernateTemplate().get(c, key);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return o;
	}

	public boolean updateSql(String sql) {
		boolean isDel = true;// 执行
		Session session = super.getHibernateTemplate().getSessionFactory().getCurrentSession();
		Query query = session.createQuery(sql);
		query.executeUpdate();
		return isDel;
	}
	public boolean updatehql(String hql) {
		boolean isDel = true;// 执行
		try {
			super.getHibernateTemplate().bulkUpdate(hql);
		} catch (Exception e) {
			e.printStackTrace();
			isDel = false;
		}
		return isDel;
	}

	public Long count(Object o) { // 统计某个实体表的行数
		Criteria criteria = super.getSessionFactory().openSession()
				.createCriteria(o.getClass());
		criteria.setProjection(Projections.rowCount());
		Long count = (Long) criteria.uniqueResult();
		return count;
	}

	public Long CountHql(String hql) { // --通过hql语句查询数据的条数
		Long count = (Long) super.getSessionFactory().openSession().createQuery(hql).uniqueResult();
		return count;
	}

	public List findpage(int offset, int length, String hql, String time,
			Class t) {
		SessionFactory sf = super.getSessionFactory();
		Session se;
		se = sf.openSession();
		Query query = se.createSQLQuery(hql).addEntity(t);
		query.setFirstResult(offset);
		query.setMaxResults(length);
		query.setParameter(0, time);// time变量的类型必须是java.sql.Date
		return query.list();
	}

	public List find(String queryString) {
		return super.getHibernateTemplate().find(queryString);
	}

	public List findfenye(int offset, int length, String hql, String time,Class t) {
		SessionFactory sf = super.getSessionFactory();
		Session se;
		se = sf.openSession();
		Query query = null;
		if (t == null) {
			query = se.createSQLQuery(hql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		} else {
			query = se.createSQLQuery(hql);
		}
		query.setFirstResult(offset);
		query.setMaxResults(length);
		query.setParameter(0, time);// time变量的类型必须是java.sql.Date
		return query.list();
	}

	public List lianhe(String sql, Class t) {
		SessionFactory sf = super.getSessionFactory();
		Session se;
		se = sf.openSession();
		Query query = null;
		if (t == null) {
			query = se.createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		} else {
			query = se.createSQLQuery(sql).addEntity(t);
		}
		return query.list();
	}
	
	public List Bysql(String sql, Class t) {
		SessionFactory sf = super.getSessionFactory();
		Session se;
		se = sf.openSession();
		Query query = null;
		if (t == null) {
			query = se.createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		} else {
			query = se.createSQLQuery(sql);
		}
		return query.list();
	}

	
	public List findname(String hql, String[] parmname, Object[] value) {   //登录检查
		return super.getHibernateTemplate().findByNamedParam(hql, parmname, value);
	}

	public List SearchInfBysql(String sql, Class t, String parm) {
		SessionFactory sf = super.getSessionFactory();
		Session se;
		se = sf.openSession();
		Query query = null;
		if (t == null) {
			query = se.createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		} else {
			query = se.createSQLQuery(sql).addEntity(t);
		}
		query.setParameter(0, parm);
		return query.list();
	}
}
